home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_540 / browser / browserii_src.lzh / Scan.c < prev    next >
C/C++ Source or Header  |  1991-07-21  |  6KB  |  222 lines

  1. /*
  2.  *    Scan.c - Copyright © 1991 by S.R. & P.C.
  3.  *
  4.  *    Created:    17 Feb 1991  10:40:09
  5.  *    Modified:    21 Jul 1991  16:16:24
  6.  *
  7.  *    Make>> make
  8.  */
  9.  
  10. #include "Global.h"
  11. #include "proto/Scan.h"
  12. #include "proto/File.h"
  13. #include "proto/String.h"
  14. #include "proto/Windows.h"
  15. #include "proto/Mouse.h"
  16. #include "proto/Draw.h"
  17. #include "proto/Sort.h"
  18.  
  19.  
  20. extern struct MinList WindowList;
  21. extern struct Config Config;
  22. extern char *ReqTitle;
  23.  
  24.  
  25. BOOL ScanDevs(void)
  26. {
  27.     struct BrowserWindow *Win;
  28.     struct DirectoryEntry *DList = NULL, *d;
  29.     struct ScrollEntry *S, *Tab;
  30.     char *sptr;
  31.     char format[10];
  32.     short i,n,len = 0;
  33.     BOOL Ok = FALSE;    /* assume the worst */
  34.  
  35.     Win = (struct BrowserWindow *)WindowList.mlh_Head;
  36.     ResetWindow(Win);
  37.     n = AddDADevs(&DList, Config.Display);
  38.     if ((Win->bw_EntryArray = AllocMem(n<<2, MEMF_PUBLIC|MEMF_CLEAR))
  39.         && (Tab = AllocMem(n*sizeof(struct ScrollEntry), MEMF_PUBLIC|MEMF_CLEAR)) ) {
  40.         d = DList;
  41.         for( i=0 ; i<n ; i++ ) {
  42.             S = &Tab[i];
  43.             Win->bw_EntryArray[i] = S;
  44.             strcpy(S->se_FileInfo.fi_Name, d->de_Name);
  45.             len = MAX(len, strlen(d->de_Name));
  46.             switch(S->se_FileInfo.fi_Type = d->de_Type) {
  47.             case DLX_ASSIGN:
  48.                 S->se_Pen = 1;
  49.                 break;
  50.             case DLX_DEVICE:
  51.                 S->se_Pen = 2;
  52.                 break;
  53.             case DLX_UNMOUNTED:
  54.             case DLX_VOLUME:
  55.                 S->se_Pen = 3;
  56.             }
  57.             d = d->de_Next;
  58.         }
  59.         FreeDAList(DList);
  60.         SPrintf(format, "%%-%ds", len);
  61.         if (sptr = AllocMem(n*(len+1), MEMF_PUBLIC)) {
  62.             for( i=0 ; i<n ; i++ ) {
  63.                 S = Win->bw_EntryArray[i];
  64.                 S->se_Print = sptr;
  65.                 SPrintf(sptr, format, S->se_FileInfo.fi_Name);
  66.                 sptr += len+1;
  67.             }
  68.             Win->bw_ShownEntries = Win->bw_NumEntries = n;
  69.             Win->bw_PrintStringLen = len;
  70.             RefreshWindow(Win);
  71.             MakeMainBottomInfoString();
  72.             Ok = TRUE;
  73.         }
  74.     }
  75.     return Ok;
  76. }
  77.  
  78.  
  79. BOOL UpdateEntryList(struct BrowserWindow *Win, BOOL BuildStrings)
  80. {
  81.     struct ScrollEntry *S, *Next;
  82.  
  83.     if (Win->bw_NumEntries > 0) {
  84.         FreeMem(Win->bw_EntryArray, Win->bw_NumEntries<<2);
  85.         Win->bw_EntryArray = NULL;
  86.     }
  87.     /* remove deleted entries */
  88.     S = (struct ScrollEntry *)Win->bw_EntryList.mlh_Head;
  89.     while(Next = (struct ScrollEntry *)S->se_Node.mln_Succ) {
  90.         if (S->se_State & STATE_DELETED) {
  91.             Win->bw_NumBytes -= S->se_FileInfo.fi_Size;
  92.             Win->bw_NumBlocks -= S->se_FileInfo.fi_NumBlocks;
  93.             if (S->se_FileInfo.fi_Type == DLX_FILE)
  94.                 Win->bw_NumFiles--;
  95.             else
  96.                 Win->bw_NumDirs--;
  97.             Remove((struct Node *)S);
  98.             CleanFileInfo(&S->se_FileInfo);
  99.             if (S->se_Print)
  100.                 FreeMem(S->se_Print, Win->bw_PrintStringLen+1);
  101.             FreeMem(S, sizeof(struct ScrollEntry));
  102.         }
  103.         S = Next;
  104.     }
  105.     Win->bw_NumEntries -= Win->bw_NumDeleted;
  106.     Win->bw_NumDeleted = 0;
  107.     /* add new entries to current list */
  108.     while(S = (struct ScrollEntry *)RemHead((struct List *)&Win->bw_NewEntryList)) {
  109.         AddTail((struct List *)&Win->bw_EntryList, (struct Node *)S);
  110.         Win->bw_NumBlocks += S->se_FileInfo.fi_NumBlocks;
  111.         Win->bw_NumBytes += S->se_FileInfo.fi_Size;
  112.         if (S->se_FileInfo.fi_Type == DLX_FILE)
  113.             Win->bw_NumFiles++;
  114.         else
  115.             Win->bw_NumDirs++;
  116.         if (BuildStrings) {
  117.             if (MatchFilters(&S->se_FileInfo, &Win->bw_FiltersInfo)) {
  118.                 if (!(S->se_Print = AllocMem(Win->bw_PrintStringLen+1, MEMF_PUBLIC)))
  119.                     return FALSE;
  120.                 BuildPrintString(Win, S);
  121.             }
  122.         }
  123.     }
  124.     Win->bw_NumEntries += Win->bw_NumNewEntries;
  125.     Win->bw_NumNewEntries = 0;
  126.     if (Win->bw_NumEntries > 0 && !(Win->bw_EntryArray = AllocMem(Win->bw_NumEntries<<2, MEMF_PUBLIC)))
  127.         return FALSE;
  128.     return TRUE;
  129. }
  130.  
  131.  
  132. BOOL List2Array(struct BrowserWindow *Win)
  133. {
  134.     struct ScrollEntry *S, *Next;
  135.     short i=0;
  136.     BOOL Ok;
  137.  
  138.     FreePrintStrings(Win);
  139.     if (Win->bw_Flags & (BWF_REBUILD_ARRAY|BWF_REBUILD_STRINGS) && !UpdateEntryList(Win, FALSE))
  140.         return FALSE;
  141.     Win->bw_MaxFilenameLen = Win->bw_NewMaxFilenameLen = 0;
  142.     Win->bw_ShownFiles = Win->bw_ShownDirs = 0;
  143.     Win->bw_ShownBytes = Win->bw_ShownBlocks = 0;
  144.     S = (struct ScrollEntry *)Win->bw_EntryList.mlh_Head;
  145.     while(Next = (struct ScrollEntry *)S->se_Node.mln_Succ) {
  146.         if (MatchFilters(&S->se_FileInfo, &Win->bw_FiltersInfo)) {
  147.             Win->bw_MaxFilenameLen = MAX(Win->bw_MaxFilenameLen, strlen(S->se_FileInfo.fi_Name));
  148.             Win->bw_EntryArray[i++] = S;
  149.             if (S->se_FileInfo.fi_Type == DLX_FILE)
  150.                 Win->bw_ShownFiles++;
  151.             else
  152.                 Win->bw_ShownDirs++;
  153.             Win->bw_ShownBytes += S->se_FileInfo.fi_Size;
  154.             Win->bw_ShownBlocks += S->se_FileInfo.fi_NumBlocks;
  155.         }
  156.         else if (S->se_State & STATE_SELECTED)
  157.             DoSelect(Win, S, OPT_TOGGLESELECT);
  158.         S = Next;
  159.     }
  160.     Win->bw_ShownEntries = i;
  161.     if (Ok = BuildPrintStrings(Win))
  162.         Sort(Win);
  163.     return Ok;
  164. }
  165.  
  166.  
  167. static BOOL RecordFile(struct BrowserWindow *Win, struct FileInfoBlock *fib)
  168. {
  169.     struct ScrollEntry *S;
  170.  
  171.     if (!(S = AllocMem(sizeof(struct ScrollEntry), MEMF_PUBLIC|MEMF_CLEAR)))
  172.         return FALSE;
  173.     Fib2Fi(&S->se_FileInfo, fib);
  174.     switch(S->se_FileInfo.fi_Type) {
  175.     case DLX_FILE:
  176.         S->se_Pen = 1;
  177.         Win->bw_NumFiles++;
  178.         break;
  179.     case DLX_DIR:
  180.         S->se_Pen = 3;
  181.         Win->bw_NumDirs++;
  182.         break;
  183.     }
  184.     Win->bw_NumEntries++;
  185.     Win->bw_NumBlocks += fib->fib_NumBlocks;
  186.     Win->bw_NumBytes += fib->fib_Size;
  187.     AddTail((struct List *)&Win->bw_EntryList, (struct Node *)S);
  188.     SPrintf(Win->bw_BottomText, " %03d %s", Win->bw_NumEntries, S->se_FileInfo.fi_Name);
  189.     RefreshBottomInfo(Win);
  190.     return TRUE;
  191. }
  192.  
  193.  
  194. BOOL ScanDir(struct BrowserWindow *Win)
  195. {
  196.     struct FileInfoBlock *fib;
  197.     BOOL Ok = TRUE;
  198.  
  199.     if (!(fib = AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR)))
  200.         return FALSE;
  201.     SetWaitPointer(TRUE);
  202.     ResetWindow(Win);
  203.     if (Examine(Win->bw_DirLock, fib) && fib->fib_DirEntryType>0) {
  204.         while (ExNext(Win->bw_DirLock, fib) && RecordFile(Win, fib));
  205.         if (IoErr() != ERROR_NO_MORE_ENTRIES)
  206.             SimpleRequest(ReqTitle, "Error reading directory\n%s.", StrIoErr());
  207.     }
  208.     FreeMem(fib, sizeof(struct FileInfoBlock));
  209.     if (Win->bw_NumEntries > 0) {
  210.         if (Win->bw_EntryArray = AllocMem(Win->bw_NumEntries<<2, MEMF_PUBLIC))
  211.             Ok = List2Array(Win);
  212.         else
  213.             Ok = FALSE;
  214.     }
  215.     MakeBottomInfoString(Win);
  216.     RefreshWindow(Win);
  217.     SetWaitPointer(FALSE);
  218.     return Ok;
  219. }
  220.  
  221.  
  222.